var http = require('../../script/api.js')
var config = require('../../script/config.js')
const App = getApp();
const innerAudioContext = wx.createInnerAudioContext();
let wavePlayInterval = null;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userinfo: null,
    WorkID: '',
    orderInfo: {
      OrderTypeName: '',
      UrgencyName: '',
      SubTypeName: '',
      CreaterName: '',
      ConsName: '',
      PhoneNumber: '',
      Description: '',
      Address: '',
      ProcedureCode: '',
      ProcedureList: [],
      FileList: []
    },
    baseUrl: config.baseUrl,
    imgList: [],
    recordPlayShow: false,
    isPlaying: false,
    wavePlayHeight: [10, 18, 24, 20, 16, 22],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options && options.WorkID) {
      this.setData({
        WorkID: options.WorkID
      })
    }
    this.getUserInfo()
    this.getOrderInfo();
    innerAudioContext.onEnded(() => {
      this.setData({
        recordPlayShow: false
      });
      clearInterval(wavePlayInterval);
      wavePlayInterval = null;
    })
  },
  getUserInfo: function () {
    try {
      var value = wx.getStorageSync(config.storageKey.userinfo);
      if (value) {
        this.setData({
          userinfo: value
        })
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (innerAudioContext) {
      innerAudioContext.destroy();
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},
  getOrderInfo: function () {
    if (this.data.WorkID == null || this.data.WorkID == '') return;
    http.GetOrderDetail({
      WorkID: this.data.WorkID,
      // ShowWeather: true,
    }).then(res => {
      if (res.code === 200) {
        this.setData({
          orderInfo: res.data
        });
        if (res.data.PFileList.length > 0) {
          const arr = [];
          for (let i = 0; i < res.data.PFileList.length; i++) {
            arr.push(this.data.baseUrl + '/' + res.data.PFileList[i].FilePath)
          }
          this.setData({
            imgList: arr
          });
        }
        // this.getOrderFiles(res.data);
        // if (res.data.IsTemperature) {
        //   this.setWorkOrderTempInfo(res.data);
        // }
        // if (res.data.IsNotHot) {
        //   this.setNotHotData(res.data);
        // }
      }
    });
  },
  // 回显图片视频
  getOrderFiles(data) {
    if (data.MFileList.length > 0) {
      this.setData({
        MFileList: data.MFileList
      });
    }
    if (data.PFileList.length > 0) {
      const arr = [];
      for (let i = 0; i < data.PFileList.length; i++) {
        arr.push(this.data.baseUrl + '/' + data.PFileList[i].FilePath)
      }
      this.setData({
        PFileList: data.PFileList,
        imgList: arr
      });
    }
  },
  // 回显不热数据
  setNotHotData(data) {
    const WorkOrderTempInfo = data.WorkOrderTempInfo;
    if (WorkOrderTempInfo) {
      this.setData({
        BedroomTemp: WorkOrderTempInfo.BedroomTemp,
        SupplyTemp: WorkOrderTempInfo.SupplyTemp,
        ReturnTemp: WorkOrderTempInfo.ReturnTemp
      });
    }
    this.setData({
      NormalFlag: data.NormalFlag == null ? '0' : data.NormalFlag,
      PetitionFlag: data.PetitionFlag == null ? '0' : data.PetitionFlag
    });
  },
  // 回显测温数据
  setWorkOrderTempInfo(data) {
    const WorkOrderTempInfo = data.WorkOrderTempInfo;
    if (WorkOrderTempInfo) {
      this.setData({
        BeginTime: WorkOrderTempInfo.BeginTime,
        EndTime: WorkOrderTempInfo.EndTime,
        OuterTemp: WorkOrderTempInfo.OuterTemp,
        DeviceTemp: WorkOrderTempInfo.DeviceTemp,
        LivingRoomTemp: WorkOrderTempInfo.LivingRoomTemp,
        BedroomTemp: WorkOrderTempInfo.BedroomTemp,
        SupplyTemp: WorkOrderTempInfo.SupplyTemp,
        ReturnTemp: WorkOrderTempInfo.ReturnTemp,
        showUserSign: WorkOrderTempInfo.UserSign ? true : false
      });
    }
    if (data.WeatherInfo) {
      this.setData({
        OuterTemp: data.WeatherInfo.Temperature
      });
    }
  },
  call: function (e) {
    var pnonenum = e.currentTarget.dataset.phone;
    var res = wx.getSystemInfoSync();
    if (res.platform == 'ios') {
      wx.makePhoneCall({
        phoneNumber: pnonenum
      });
    } else {
      wx.showActionSheet({
        itemList: [pnonenum, '呼叫'],
        success: function (res) {
          if (res.tapIndex == 1) {
            wx.makePhoneCall({
              phoneNumber: pnonenum
            })
          }
        }
      })
    }
  },
  previewImage: function (e) {
    //预览图片
    const that = this;
    var index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: that.data.imgList[index],
      urls: that.data.imgList
    });
  },
  downloadFile: function (e) {
    var that = this;
    var filePath = e.currentTarget.dataset.url; //对应的网络路径，可以是内网的或者外网
    // var fileType = e.currentTarget.dataset.type;
    wx.downloadFile({ //下载对应文件
      url: filePath,
      success: function (res) {
        var filePath = res.tempFilePath; //文件路径
        wx.openDocument({
          filePath: filePath, // 装载对应文件的路径
          // fileType: fileType, // 指定打开的文件类型
          showMenu: true, // 右上角的菜单转发分享操作
          success: function (res) {
            console.log("打开成功");
          },
          fail: function (res) {
            console.log(res);
          }
        })
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },

  // 播放录音
  playRecording(e) {
    this.setData({
      recordPlayShow: true,
      isPlaying: true
    })
    this.setPlayingInterval();

    let url = this.data.baseUrl + '/' + e.currentTarget.dataset.url;
    innerAudioContext.src = url;
    innerAudioContext.play();
  },
  setPlayingInterval() {
    wavePlayInterval = setInterval(() => {
      const waveHeight = [...this.data.wavePlayHeight];
      const arr = waveHeight.map(() => {
        return Math.floor(Math.random() * 10) + 10
      });
      this.setData({
        wavePlayHeight: arr
      })
    }, 200)
  },
  playControl() {
    if (!innerAudioContext) return
    if (this.data.isPlaying) {
      clearInterval(wavePlayInterval);
      wavePlayInterval = null;
      innerAudioContext.pause();
    } else {
      this.setPlayingInterval();
      innerAudioContext.play();
    }
    this.setData({
      isPlaying: !this.data.isPlaying
    });
  },
  closePlaying() {
    innerAudioContext.stop();
    clearInterval(wavePlayInterval);
    wavePlayInterval = null;
    this.setData({
      recordPlayShow: false,
      isPlaying: false
    });
  },
})